//
//  ViewController.swift
//  NewDelivery
//
//  Created by 天下宅 on 2016/12/16.
//  Copyright © 2016年 天下宅. All rights reserved.
//

import UIKit
import SwiftyJSON
class IncomeVC: BaseVC {

    
    var startDateBtn = DateBtn.init(withTitle: "起始日期：")
    var endDateBtn = DateBtn.init(withTitle: "截至日期：")
    
    var searchBtn  = UIButton.init(type: UIButtonType.custom)

    var orderCountLabel = UILabel()
    var incomeLabel = UILabel()
    override func viewDidLoad() {
        super.viewDidLoad()

        let nav = WKNavView.creatNavView(WithTitle: "配送费统计", vProtocol: self)
        nav.leftBtnEnable = true
        nav.leftImage = UIImage.init(named: "左箭头")
        self.view.addSubview(nav)
        // Do any additional setup after loading the view.
        //共收入
        
        self.view.backgroundColor = UIColor.groupTableViewBackground
        
        self.view.addSubview(startDateBtn)
        startDateBtn.snp.makeConstraints { (make) in
            make.left.equalTo(self.view).offset(20)
            make.top.equalTo(nav.snp.bottom)
            make.height.equalTo(30)
            make.width.equalTo(screenWidth - 60)
        }
        let sevenDayAgo = String.timeStampToString(timeS: Date.init(timeIntervalSinceNow: -(24*60*60*7)).timeIntervalSince1970 * 1000, dateStyle: "yyyy-MM-dd")
        startDateBtn.date =  sevenDayAgo
        let now = String.timeStampToString(timeS: Date().timeIntervalSince1970 * 1000, dateStyle: "yyyy-MM-dd")
        endDateBtn.date = now;
        self.view.addSubview(endDateBtn)
        endDateBtn.snp.makeConstraints { (make) in
            make.left.equalTo(self.view).offset(20)
            make.top.equalTo(startDateBtn.snp.bottom)
            make.height.equalTo(30)
            make.width.equalTo(screenWidth - 60)
        }
        
        let orderHintLbale = UILabel()
        orderHintLbale.text = "订单数量"
        self.view.addSubview(orderHintLbale)
        orderHintLbale.snp.makeConstraints { (make) in
            make.left.equalTo(self.view)
            make.right.equalTo(self.view.snp.centerX)
            make.top.equalTo(endDateBtn.snp.bottom).offset(20)
            make.height.equalTo(30)
        }
        
        self.view.addSubview(orderCountLabel)
        orderCountLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.view)
            make.right.equalTo(self.view.snp.centerX)
            make.top.equalTo(orderHintLbale.snp.bottom).offset(10)
            make.height.equalTo(30)
        }
        orderCountLabel.text = "123"
        
        
        let incomeHintLabel = UILabel()
        incomeHintLabel.text = "收入"
        self.view.addSubview(incomeHintLabel)
        incomeHintLabel.snp.makeConstraints { (make) in
            make.right.equalTo(self.view)
            make.left.equalTo(self.view.snp.centerX)
            make.top.equalTo(endDateBtn.snp.bottom).offset(20)
            make.height.equalTo(30)
        }
        
        self.view.addSubview(incomeLabel)
        incomeLabel.snp.makeConstraints { (make) in
            make.left.right.height.equalTo(incomeHintLabel)
            make.top.equalTo(incomeHintLabel.snp.bottom).offset(10)
        }
        incomeLabel.text = "456"

        
        orderHintLbale.textAlignment = NSTextAlignment.center
        incomeHintLabel.textAlignment = NSTextAlignment.center
        incomeLabel.textAlignment = NSTextAlignment.center
        orderCountLabel.textAlignment = NSTextAlignment.center

        self.view.addSubview(searchBtn)
        searchBtn.snp.makeConstraints { (make) in
            make.top.equalTo(nav.snp.bottom).offset(5)
            make.right.equalTo(self.view).offset(-10)
            make.width.equalTo(60)
            make.bottom.equalTo(endDateBtn).offset(-5)
        }
        searchBtn.setTitleColor(UIColor.white, for: UIControlState.normal)
        searchBtn.setTitle("搜索", for: UIControlState.normal)
        searchBtn.backgroundColor = MainColor
        searchBtn.addTarget(self, action: #selector(startSearch), for: .touchUpInside)
        searchBtn.layer.cornerRadius = 5
        
        self.startSearch()
    }
    func startSearch()  {
        
        let f = DateFormatter()
        f.dateFormat = "yyyy-MM-dd"
        let start = f.date(from: startDateBtn.date) ?? Date()
        let end = f.date(from: endDateBtn.date) ?? Date()
        if start.timeIntervalSince1970 > end.timeIntervalSince1970  {
            self.view.wkMakeToastToCenter(message: "截止时间应大于起始时间");
            return;
        }
        
        NetWorkingManager.getIncome(startDateBtn.date, endDate: endDateBtn.date, hintStr: "请稍候", block: {(result:(state:WKNetWorkState,data:JSON?)) in
            switch result.state
            {
            case .Faild(let message):
                self.view.wkMakeToastToCenter(message: message)
            case .Success:
                print(result.data!.dictionaryValue)
                let dict = result.data!.dictionaryValue
                self.orderCountLabel.text = "\(dict["ondayshuliang"]!.intValue)"
                self.incomeLabel.text =  "\(dict["ondaycost"]!.doubleValue)"

            }
})
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
